/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package org.moon.framework.autoconfigure.swagger;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.moon.framework.autoconfigure.MoonConstants;
import org.moon.framework.autoconfigure.swagger.properties.SwaggerProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author moon
 */
@EnableSwagger2
@Configuration
@ConditionalOnClass(Docket.class)
@Profile({"local", "dev", "test"})
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration {

    @Bean
    public Docket createRestApi(SwaggerProperties swaggerProperties, ApiInfo apiInfo) {

        //添加head参数start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name(MoonConstants.HEADER_TENANT_CODE).description("租户").defaultValue(MoonConstants.DEFAULT_TENANT_CODE).modelRef(new ModelRef("string")).parameterType("header").required(true).build();
        pars.add(tokenPar.build());
        tokenPar.name(MoonConstants.HEADER_ACCESS_TOKEN).description("令牌").defaultValue(swaggerProperties.getAccessToken()).modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        tokenPar.name(MoonConstants.REQUEST_FROM).description("请求来源").defaultValue(MoonConstants.RequestFormEnum.platform.name()).modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .globalOperationParameters(pars)
                .select()
                .apis(Optional.ofNullable(swaggerProperties.getBasePackage())
                        .map(RequestHandlerSelectors::basePackage).orElse(RequestHandlerSelectors.any()))
                .paths(Optional.ofNullable(swaggerProperties.getPaths()).map(PathSelectors::regex)
                        .orElse(PathSelectors.any()))
                .build();
    }

    @Bean(name = "swagger2ApiInfo")
    public ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
        return new ApiInfoBuilder()
                .title(swaggerProperties.getTitle()) // 标题
                .description(swaggerProperties.getDescription()) // 描述
                .termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl()) //网址
                .version(swaggerProperties.getVersion()) // 版本号
                .contact(new Contact(swaggerProperties.getContactName(),
                        swaggerProperties.getContactUrl(), swaggerProperties.getContactEmail()))
                .license(swaggerProperties.getLicense())
                .licenseUrl(swaggerProperties.getLicenseUrl())
                .build();
    }
}